#include "Din_Task.h"
#include "CD4051.h"
#include "Input.h"
#include "PWM_OUT.h"
#include "SGM51242.h"
#include "Timer.h"
#include "W25QXX.h"
#include "core_cm3.h"
#include "modbus.h"
#include "sdp800.h"
void Din_Task(void* pdata) {
    PWM_SetDuty(PWM_CHANNEL_1, 100);
    PWM_SetDuty(PWM_CHANNEL_2, 90);
    PWM_SetDuty(PWM_CHANNEL_3, 80);
    PWM_SetDuty(PWM_CHANNEL_4, 70);
    PWM_SetDuty(PWM_CHANNEL_5, 60);
    PWM_SetDuty(PWM_CHANNEL_6, 50);

    for (;;) {
        uint8_t cal_num = 0;
        sdp800_proc(cal_num);
        Input.analog.pre = get_sdp800_value(cal_num);   // 获取静压值
        SGM51242_Proc();
        for (uint8_t i = 0; i < CHIP_NUM; i++) {
            CD4051_proc(i);
        }

        for (uint8_t i = 0; i < DIN_NUM; i++) {                // 数字输入部分
            Input.din[i].cur_state     = Get_input_state(i);   // 获取输入状态
            Input.din[i].cur_count     = Get_pulse_count(i);   // 获取脉冲个数
            Input.din[i].cur_closetime = Get_active_time(i);   // 获取吸合时长
        }
        for (uint8_t i = 0; i < ANALOG_NUM; i++) {   // 模拟输入部分
            Input.analog.ad_value[i] = Get_analog(i);
        }
        OSTimeDly(10);
    }
}
/*获取滤波后AD*/
uint16_t Get_analog(uint8_t ch) {
    uint16_t val = 0;
    if (ch <= 6) {
        val = averages[ch];
    } else if (ch <= 14) {
        val = Get_CD4051(1, ch - 6);
    } else if (ch <= 22) {
        val = Get_CD4051(0, ch - 14);
    }
    return val;
}
